package com.tentact.dao;

import com.tentact.vo.Chf_DEId;
import com.tentact.vo.Meeting;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class Chf_MeetingDao {
    //添加Meeting表数据
    public int addMeeting(String meeting_name,int h_number,String startTime,String endTime,int roomid,String description, int reservationistid){
        String sql="INSERT INTO meeting(MeetingName,RoomId,ReservationistId,NumOfParticipants,BeginTime,EndTime,ReservationTime,CanceledTime,MeetingStatus,Description)VALUES(?,?,?,?,?,?,?,null,1,?)";
        Date date1=new Date();
        Date date2=new Date();
        Object[] para={meeting_name,roomid,reservationistid,h_number,changeDate(startTime),changeDate(endTime),date1,description};

        return  Dbutil.executeUpdate(sql,para);

    }
    //去除日期数据中的“T”
    public  String changeDate(String date){
        String new_string=date.replace("T"," ");
        return  new_string;
    }
    //通过会议名查找ID
    public  List<Meeting> selectMeetingID(String meetingName){
        String sql="SELECT m.MeetingId FROM meeting m WHERE m.MeetingName = ?";
        Object[] para= {meetingName};
        RowMapper<Meeting> rm=rs -> {
            Meeting meeting=new Meeting();
            try {
             meeting.setMeetingid(rs.getInt("MeetingId"));


            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return meeting;
        };
        return  Dbutil.executeQuery(sql,para,rm);

    }
    //查找所有会议名
    public  List<Meeting> selectMeetingAllName(){
        String sql="SELECT m.MeetingName FROM meeting m ";
        Object[] para=null;
        RowMapper<Meeting> rm=rs -> {
            Meeting meeting=new Meeting();
            try {
                meeting.setMeetingname(rs.getString("MeetingName"));


            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            return meeting;
        };
        return  Dbutil.executeQuery(sql,para,rm);

    }
    //判断会议名是否存在
    public  boolean judgeName(String meeting_name){
        for (Meeting e:selectMeetingAllName()
             ) {
            if(e.getMeetingname().equals(meeting_name)){
                return true;
            }
        }
        return false;
    }
    //获取单个int型ID
    public  int meetingID(String meetingName){
        int meetingId=0;
        for (Meeting e:selectMeetingID(meetingName)) {
           meetingId=e.getMeetingid();
        }
        return meetingId;
    }
    //添加meetingparticipants表数据
    public int addMeetingParticipant(int MeetingId,int ParticipantId){
        String sql="INSERT INTO meetingparticipants(MeetingId,ParticipantId)VALUES (?,?)";

        Object[] para={MeetingId,ParticipantId};

        return  Dbutil.executeUpdate(sql,para);

    }

}
